package com.ruoyix.aicode.controller;

import cn.dev33.satoken.annotation.SaIgnore;

import com.ruoyix.aicode.domain.CodeGenerationRequest;
import com.ruoyix.aicode.service.FrontCodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

/**
 * 前端代码生成控制器
 */
@RestController
@RequestMapping("/aicode/frontcode")
@CrossOrigin(origins = "*")
public class FrontCodeController {

    private static final Logger logger = LoggerFactory.getLogger(FrontCodeController.class);
    private final FrontCodeService frontCodeService;

    @Autowired
    public FrontCodeController(FrontCodeService frontCodeService) {
        this.frontCodeService = frontCodeService;
    }

    @SaIgnore
    @PostMapping(value = "/generate", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> generateCode(@RequestBody CodeGenerationRequest request) {
        // 调用服务生成代码
        logger.info("收到前端代码生成请求，需求: {}", request.getRequirements());
        return frontCodeService.generateCode(request);
    }
}
